package controller;

import bean.Account;
import dao.AccountDao;
import dao.impl.AccountDaoImpl;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import util.AlertUtil;
import util.FxmlUtil;

import java.util.Optional;

public class TransferAccController {
    public TextField toNo;
    public TextField money;
    private AccountDao accountDao = new AccountDaoImpl();

    public void cancelAction() {
        Stage stage = (Stage) this.money.getScene().getWindow();
        Scene scene = FxmlUtil.SCENE.get("main");
        stage.setScene(scene);
        stage.setTitle("ATM - 首页");
    }

    public void okAction() {
        String toNoText = toNo.getText();
        float moneyText = Float.parseFloat(money.getText());

        Account account = (Account) FxmlUtil.DATA.get("account");
        int num = accountDao.transferAcc(account.getAccNo(), toNoText, moneyText);
        if (num > 0){
            Optional<ButtonType> success = AlertUtil.alert(Alert.AlertType.INFORMATION, "转账成功",
                    "转账成功，是否继续操作？",ButtonType.YES,ButtonType.NO);
            if (success.isPresent() && success.get() == ButtonType.NO){
                cancelAction();
            }
        }else {
            Optional<ButtonType> success = AlertUtil.alert(Alert.AlertType.INFORMATION, "转账失败",
                    "转账失败，是否继续操作？",ButtonType.YES,ButtonType.NO);
            if (success.isPresent() && success.get() == ButtonType.NO){
                cancelAction();
            }
        }
    }

    public void resetAction() {
        this.toNo.setText("");
        this.money.setText("");
    }
}
